﻿// See https://aka.ms/new-console-template for more information


using System.Data.SqlClient;
using System.Text;



List<User> users = new List<User>();
for (int i = 0; i < 10000; i++)
{
    users.Add(new User
    {
        Id = Guid.NewGuid().ToString(),
        Name = "Name" + i,
        Age = i,
        Gender = i % 2,
        Email = "email" + i + "@test.com",
        Password = Guid.NewGuid().ToString(),
        CreatedTime = DateTime.Now,
    });
}
DateTime dt = DateTime.Now;
//foreach (var user in users)
//{
//    Insert(user);
//}
Inserts(users);
DateTime dt2 = DateTime.Now;
Console.WriteLine(dt);
Console.WriteLine(dt2);
Console.ReadLine();

static void Insert(User data)
{
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
    builder.DataSource = "LAPTOP-DM7G1PLH\\SQLEXPRESS";
    builder.InitialCatalog = "Test";
    builder.UserID = "sa";
    builder.Password = "111111";
    string connStr = builder.ToString();
    SqlConnection conn = new SqlConnection(connStr);
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = @$"INSERT INTO [dbo].[Table_1]
           ([Id]
           ,[Name]
           ,[Age]
           ,[Gender]
           ,[Email]
           ,[Password]
           ,[CreatedTime])
     VALUES
           ('{data.Id}'
           ,'{data.Name}'
           ,{data.Age}
           ,{data.Gender}
           ,'{data.Email}'
           ,'{data.Password}'
           ,'{data.CreatedTime.ToString("yyyy-MM-dd HH:mm:ss")}');";

    cmd.CommandType = System.Data.CommandType.Text;
    conn.Open();
    Console.WriteLine("开始："+DateTime.Now);
    int row = cmd.ExecuteNonQuery();
    if (row > 0)
    {
        Console.WriteLine(data.Age);
    }
    else
    {
        Console.WriteLine(false);
    }
    conn.Close();
}

static void Inserts(List<User> datas)
{
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
    builder.DataSource = "LAPTOP-DM7G1PLH\\SQLEXPRESS";
    builder.InitialCatalog = "Test";
    builder.UserID = "sa";
    builder.Password = "111111";
    string connStr = builder.ToString();
    SqlConnection conn = new SqlConnection(connStr);
    SqlCommand cmd = conn.CreateCommand();
    
    StringBuilder sb = new StringBuilder();
    foreach (var data in datas)
    {
        sb.Append(@$"INSERT INTO [dbo].[Table_1]
           ([Id]
           ,[Name]
           ,[Age]
           ,[Gender]
           ,[Email]
           ,[Password]
           ,[CreatedTime])
     VALUES
           ('{data.Id}'
           ,'{data.Name}'
           ,{data.Age}
           ,{data.Gender}
           ,'{data.Email}'
           ,'{data.Password}'
           ,'{data.CreatedTime.ToString("yyyy-MM-dd HH:mm:ss")}');");
    }

    

    cmd.CommandText = sb.ToString();
    Console.WriteLine("开始：" + DateTime.Now);
    cmd.CommandType = System.Data.CommandType.Text;
    conn.Open();
    int row = cmd.ExecuteNonQuery();
    if (row > 0)
    {
        Console.WriteLine("success");
    }
    else
    {
        Console.WriteLine(false);
    }
    conn.Close();
}

class User
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int Gender { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public DateTime CreatedTime { get; set; }
}